/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.leftPanels;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import view.main.MainGUI;
import weka.core.Instances;
import model.statistic.*;

/**
 *
 * @author Stupi
 */
public class DescriptivePnlLeft extends javax.swing.JPanel {

    private ArrayList<Integer> indices;
    private ArrayList<String> attributes;
    private Instances instances;

    private boolean hasNumeric;

    /**
     * Creates new form DescriptivePnlLeft
     */
    public DescriptivePnlLeft(Instances instances1) {
        initComponents();
        setSize(MainGUI.getInstance().getPnlLeft().getWidth(), MainGUI.getInstance().getPnlLeft().getHeight());
        setVisible(true);
        
        instances = instances1;
        attributes = new ArrayList<>();
        indices = new ArrayList<>();
        
        int numAttributes = instances.numAttributes();
        for (int i = 0; i < numAttributes; i++) {
            if (instances.attribute(i).isNumeric()) {
                attributes.add(instances.attribute(i).name());
                indices.add(i);
            }
        }
        hasNumeric = false;
        for (int i = 0; i < attributes.size(); i++) {
            cmbAttributes.addItem(attributes.get(i));
            hasNumeric = true;
        }
        
        
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cmbAttributes = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        checkArithmetic = new javax.swing.JCheckBox();
        checkGeometric = new javax.swing.JCheckBox();
        checkHarmonic = new javax.swing.JCheckBox();
        checkMode = new javax.swing.JCheckBox();
        checkMedian = new javax.swing.JCheckBox();
        checkVariance = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        btnStart = new javax.swing.JButton();

        jLabel1.setText("Select attribute:");

        jLabel2.setText("Select method of");

        jLabel3.setText("descriptive statistic:");

        checkArithmetic.setText("Arithmetic mean");

        checkGeometric.setText("Geometric mean");

        checkHarmonic.setText("Harmonic mean");

        checkMode.setText("Mode");

        checkMedian.setText("Median");

        checkVariance.setText("Variance");

        textArea.setColumns(20);
        textArea.setRows(5);
        jScrollPane1.setViewportView(textArea);

        btnStart.setText("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(checkVariance)
                            .addComponent(checkMedian)
                            .addComponent(checkMode)
                            .addComponent(checkGeometric)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cmbAttributes, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
                            .addComponent(jLabel3)
                            .addComponent(checkArithmetic)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(btnStart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(checkHarmonic, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbAttributes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(checkArithmetic)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkGeometric)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkHarmonic)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkMode)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkMedian)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkVariance)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnStart)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        
        if (!hasNumeric) {
            JOptionPane.showMessageDialog(null, "In table are only nominal attributes.");
            return;
        }
        
        String name = cmbAttributes.getSelectedItem().toString();
        
        int index = 0;
        for (int i = 0; i < attributes.size(); i++) {
            if (attributes.get(i).equals(name)) {
                index = indices.get(i);
                break;
            }
        }
        
        int n = instances.numInstances();
        double [] elements = new double[n];
        for (int i = 0; i < n; i++) {
            double element = instances.instance(i).value(index);
            elements[i] = element;
            
        }
        

        ArrayList<String> labels = new ArrayList<>();
        
        
        
        MeasureCentral m = new MeasureCentral();
        SquaredError s = new SquaredError();
        
        if (checkArithmetic.isSelected()) {
            double result = m.arithmeticMean(elements);
            labels.add("Arithmetic Mean: "+result);
        }
        if (checkGeometric.isSelected()) {
            double result = m.geometricMean(elements);
            labels.add("Geometric Mean: "+result);
        }
        if (checkHarmonic.isSelected()) {
            double result = m.harmonicMean(elements);
            labels.add("Harmonic Mean: "+result);
        }
        if (checkMode.isSelected()) {
            double result = m.mode(elements);
            labels.add("Mode: "+result);
        }
        if (checkMedian.isSelected())  {
            double result = m.median(elements);
            labels.add("Median: "+result);
        }
        if (checkVariance.isSelected()) {
            double result = s.variance(elements);
            labels.add("Variance: "+result);
        }
        
        if (labels.size()>0) {
            textArea.setText(labels.get(0)+"\n");
        }
        for (int i = 1; i < labels.size(); i++) {
            textArea.setText(textArea.getText()+labels.get(i)+"\n");
        }
        
        
        
        
        
        
    }//GEN-LAST:event_btnStartActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStart;
    private javax.swing.JCheckBox checkArithmetic;
    private javax.swing.JCheckBox checkGeometric;
    private javax.swing.JCheckBox checkHarmonic;
    private javax.swing.JCheckBox checkMedian;
    private javax.swing.JCheckBox checkMode;
    private javax.swing.JCheckBox checkVariance;
    private javax.swing.JComboBox cmbAttributes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables
}
